using System;
using System.Collections;
using System.Collections.Generic;
using CthulhuDomain.GameEntities;
using CthulhuDomain.Services;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Linq;

namespace CthulhuDomain.StageManagement
{
  public class Menu
  {
    private MenuCommandFactory CommandFactory { get; set; }

    public Menu(IGameServiceProvider gameServiceProvider, IRectangle menuItemDim) {
      LinkedInfoLoaded = eLinkedInfoLoaded.None;
      GameServiceProvider = gameServiceProvider;
      MenuItems = new LinkedList<MenuItem>();
      CommandFactory = MenuCommandFactory.GetFactory(MenuCommandFactory.eTypeFactory.Start);
      MenuItems.AddFirst(new MenuItem(gameServiceProvider) {
        Command = CommandFactory.CreateCommand(gameServiceProvider),
        BackgroundColor = new ColorAdapter(Color.BurlyWood),
        ForeColor = new ColorAdapter(Color.White),
        Dimension = menuItemDim,
        Text = " Start",
        Selected = true
      });
      CommandFactory = MenuCommandFactory.GetFactory(MenuCommandFactory.eTypeFactory.Exit);
      MenuItems.AddLast(new MenuItem(gameServiceProvider) {
        Command = CommandFactory.CreateCommand(gameServiceProvider),
        BackgroundColor = new ColorAdapter(Color.BurlyWood),
        ForeColor = new ColorAdapter(Color.White),
        Dimension = menuItemDim,
        Text = " Exit",
        Selected = false
      });
    }

    [Flags]
    public enum eLinkedInfoLoaded : long
    {
      None = 0,
      Content = 1 << 0,
    }
    public static eLinkedInfoLoaded LinkedInfoLoaded { get; set; }

    protected static IGraphicResource BackGroundTexture { get; set; }
    protected static IGameServiceProvider GameServiceProvider { get; set; }
    public static IGraphicResource Font { get; set; }

    private static LinkedList<MenuItem> MenuItems { get; set; }

    public void Update(IRectangle context, Keys[] keys, IGameTime gameTime) {
      EnsureContent();
      var selected = GetSelectedItem();

      if (((IList)keys).Contains(Keys.Up))
        SelectPreviousItem(selected);
      else if (((IList)keys).Contains(Keys.Down))
        SelectNextItem(selected);

      foreach (var item in MenuItems)
        item.Update(keys);
    }

    private static void SelectPreviousItem(MenuItem selected) {
      if (MenuItems.Find(selected).Previous == null) return;
      selected.Selected = false;
      var linkedListNode = MenuItems.Find(selected).Previous;
      if (linkedListNode != null) linkedListNode.Value.Selected = !selected.Selected;
    }

    private static void SelectNextItem(MenuItem selected) {
      if (MenuItems.Find(selected).Next == null) return;
      selected.Selected = false;
      var linkedListNode = MenuItems.Find(selected).Next;
      if (linkedListNode != null) linkedListNode.Value.Selected = !selected.Selected;
    }

    private static MenuItem GetSelectedItem() {
      return MenuItems.SingleOrDefault(x => x.Selected);
    }

    protected RectangleAdapter BackgroundRectangle { get; set; }
    protected Vector2Adapter BackgroundPosition { get; set; }

    public void EnsureContent() {
      if (Convert.ToBoolean(LinkedInfoLoaded & eLinkedInfoLoaded.Content)) return;
      BackgroundPosition = new Vector2Adapter(0, 0);
      BackgroundRectangle = new RectangleAdapter(0, 0, 0, 0);
      BackGroundTexture = GameServiceProvider.ContentManager.LoadTexture("px");
      Font = GameServiceProvider.ContentManager.LoadFont("Impact");
      var count = 0;
      foreach (var item in MenuItems) {
        item.Font = Font;
        item.Position = new Vector2Adapter(GameServiceProvider.Context.Width / 2 - item.Dimension.Width,
                                           (GameServiceProvider.Context.Height -
                                            (MenuItems.Count * item.Dimension.Height)) / 2 +
                                           (count * item.Dimension.Height));
        if (count == 0) {
          BackgroundPosition = new Vector2Adapter(item.Position.X - 50, item.Position.Y - 50);
          BackgroundRectangle = new RectangleAdapter(0, 0, item.Dimension.Width + 150, item.Dimension.Height * MenuItems.Count + 100);
        }
        count++;
      }

      LinkedInfoLoaded |= eLinkedInfoLoaded.Content;
    }

    public void Draw() {
      EnsureContent();
      var background = new DrawableRectangle(BackGroundTexture, BackgroundPosition,
                                             BackgroundRectangle,
                                             new ColorAdapter(Color.GreenYellow));
      GameServiceProvider.SpriteBatch.Draw(background.Texture, background.Position, background.DestinationRectangle, background.Color);

      foreach (var item in MenuItems)
        item.Draw();
    }
  }
}